from random import randint
import plotly.express as px

class Die:
    """表示一个骰子的类。"""

    def __init__(self, num_sides=6):
        """默认假设为一个六面骰子。"""
        self.num_sides = num_sides

    def roll(self):
        """返回一个1到骰子面数之间的随机数。"""
        return randint(1, self.num_sides)

# 创建一个D6并掷100次来分析频率
die = Die()
results_100 = []
for roll_num in range(100):
    result = die.roll()
    results_100.append(result)

# 分析100次掷骰子的结果
frequencies_100 = []
possible_results = range(1, die.num_sides + 1)  # 可能的结果范围
for value in possible_results:
    frequency = results_100.count(value)
    frequencies_100.append(frequency)

print("100次掷骰子的结果:", results_100)
print("100次掷骰子的频率:", frequencies_100)

# 可视化100次掷骰子的结果（可选，用于演示）
fig_100 = px.bar(x=possible_results, y=frequencies_100, title="掷一个D6 100次的结果")
fig_100.show()  # 取消注释以显示图表

# 创建一个D6并掷1000次以可视化频率
results_1000 = []
for roll_num in range(1000):
    result = die.roll()
    results_1000.append(result)

# 分析1000次掷骰子的结果
frequencies_1000 = []
for value in possible_results:
    frequency = results_1000.count(value)
    frequencies_1000.append(frequency)

# 可视化1000次掷骰子的结果
title = "掷一个D6 1000次的结果"
labels_dict = {'x': '结果', 'y': '结果的频率'}  # 标签字典
fig_1000 = px.bar(x=possible_results, y=frequencies_1000, title=title, labels=labels_dict)
fig_1000.show()  # 显示图表